package com.jed.xxh.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jed.xxh.R;
import com.jed.xxh.adapter.PhotoAdapter;
import com.jed.xxh.entity.PhotoData;
import com.jed.xxh.utils.L;
import com.jed.xxh.utils.PicassoUtil;
import com.jed.xxh.utils.StaticClass;
import com.jed.xxh.utils.T;
import com.jed.xxh.view.CustomDialog;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 项目名：stewardAPP<br/>
 * 包名：  com.jed.xxh.fragment<br/>
 * 文件名：GirlFragment<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/24 14:05<br/>
 * 描述：图片管理<br/>
 */
public class PhotoFragment extends Fragment {
    private GridView mGridView;
    private List<PhotoData> mList = new ArrayList<>();
    private PhotoAdapter mAdapter;
    private PhotoData data;
    private CustomDialog dialog;
    private ImageView iv_img;

    private PhotoViewAttacher attacher;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mGridView = view.findViewById(R.id.mGridView);
        dialog = new CustomDialog(getActivity(),
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT,
                R.layout.dialog_img,
                R.style.Theme_dialog,
                Gravity.CENTER,
                R.style.pop_anim_style);
        iv_img  =dialog.findViewById(R.id.iv_img);

        getPhoto();
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                data = mList.get(i);
                PicassoUtil.loadImgView(data.getUrl(),iv_img);

                attacher = new PhotoViewAttacher(iv_img);
                attacher.update();
                dialog.show();

            }
        });

    }

    /**
     * 获取图片
     */
    private void getPhoto() {
        RxVolley.get(StaticClass.BAIDU_GIRL_URL, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                L.i("图片管理加载的Json：" + t);
                try {
                    JSONObject jsonObject = new JSONObject(t);
                    JSONArray imgs = jsonObject.getJSONArray("imgs");

                    for (int i = 0; i < imgs.length() - 1; i++) {
                        JSONObject jsonObject1 = imgs.getJSONObject(i);
                        data = new PhotoData();
                        data.setUrl(jsonObject1.getString("imageUrl"));
                        mList.add(data);
                    }
                    mAdapter = new PhotoAdapter(mList);
                    mGridView.setAdapter(mAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
